/* 快速找到和为0的4个数 */

#include<algorithm>
#include<iostream>
using namespace std;

constexpr int N = 6;

struct TSum
{
	int x;
	int y;
	int sum;
};

bool operator<(const TSum &lhs, const TSum &rhs)
{
	return lhs.sum < rhs.sum;
}

struct Comp
{
	bool operator()(int value, const TSum &lhs)
	{
		return value < lhs.sum;
	}
	bool operator()(const TSum &lhs, int value)
	{
		return lhs.sum < value;
	}
};

int main(void)
{
	int a[N] = {-45, -41, -36, -36, 26, -32};
	int b[N] = {22, -27, 53, 30, -38, -54};
	int c[N] = {42, 56, -37, -75, -10, -6};
	int d[N] = {-16, 30, 77, -46, 62, 45};

	TSum sumAB[N*N]{0};
	TSum sumCD[N*N]{0};
	
	int idx = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			sumAB[idx].x = a[i];
			sumAB[idx].y = b[j];
			sumAB[idx].sum = a[i] + b[j];

			sumCD[idx].x = c[i];
			sumCD[idx].y = d[j];
			sumCD[idx].sum = c[i] + d[j];
			idx++;
		}
	}

	sort(sumAB, sumAB + N * N);
	sort(sumCD, sumCD + N * N);

	for (int i = 0; i < N * N; i++)
	{
		TSum *ptLeft = &sumAB[i];
		auto p = equal_range(sumCD, sumCD + N * N, -ptLeft->sum, Comp{});
		{
			for (auto i = p.first; i != p.second; ++i)
			{
				cout<< ptLeft->x << " " << ptLeft->y << " " << i->x << " " << i->y <<endl;
			}
		}
	}

	return 0;
}
